﻿var blnAlreadyRh = false;
var blnGjxwReq = false; //关键行为是否必填
var blnYxYeTai = false; //需求业态是否必填
var blnYxFangXing = false; //需求户型是否必填
var blnYxArea = false; //需求面积是否必填
var blnLxfs = false; //联系方式是否必填
var blnNotFresh = false; //不刷新页面。
var blnFormChanged; //判断 checkbox 和业态需求是否修改
var blnGzfmReq = false;

//页面初始化
function Init() {
    
    //保存客户状态和购房意向的初始值
    sGlobalGfyx = appForm.Gfyx.value;
    sGlobalStatus = appForm.Status.value;
    //得到“回访追访时间设置”中已设置的最高级别状态
    var oProjGUID = document.getElementById("__ProjGUID");
    if (oProjGUID && oProjGUID.value) {
        sGlobalMaxStatus = GetDataFromXMLHTTP("/SLXT/PUB/Pub_XMLHTTP.aspx?ProjGUID=" + oProjGUID.value, "GetMaxHfTimeSetState");
    }
    //联系方式
    if (__mode.value == 1)
    {
        SetControlValue("chkLxfs", "手机;短信");
    }
    else
    {       
        SetControlValue("chkLxfs", appForm.appForm_Lxfs.value);
    }

    appForm.appForm_UserName.setReadOnly(true, false);

    if (__mode.value == 3 || appForm.Status.value == "丢失")
    {
        document.getElementById("btnSave").style.display = "none";       
    }
    CanEditCstName();

    if (document.getElementById("FirstName") != null)
    {
        document.getElementById("FirstName").setAttribute("forbiddenChars", "@%#$*?-|,;'\"&<>^[]");
    }

    if (document.getElementById("LastName") != null)
    {
        document.getElementById("LastName").setAttribute("forbiddenChars", "@%#$*?-|,;'\"&<>^[]");
    }

    if (document.getElementById("CstName") != null)
    {
        document.getElementById("CstName").setAttribute("forbiddenChars", "@%#$*?-|,;'\"&<>^[]");
    }
    
    //不从来电来访到此页面时
    try
    {
        if (document.getElementById("__CstType") != null)
        {
            if (document.getElementById("__CstType").value == "公司")
            {
                appForm.CstName.focus();
            }
            else
            {
                appForm.FirstName.focus();
            }
        }
    }
    catch (ex) { }    
}

//更改购房意向
function gfyxChange(blnOnLoad) {
    if (blnOnLoad == undefined || blnOnLoad == null)
    {
        blnOnLoad = false;
    }
    setAllControlReqFalse();
    if (document.getElementById("Gzfm1") != null) {
        blnGzfmReq = false;
        document.getElementById("Gzfm1").parentNode.previousSibling.style.fontWeight = "";
        document.getElementById("Gzfm1").parentNode.previousSibling.style.color = "black";
    } 
    var sGfyx = appForm.Gfyx.value;
    if (event.srcElement != null && event.srcElement.name == "appForm_Gfyx")
    {
        sGfyx = event.srcElement.value;
    }
    
    if (document.getElementById("__OppGUID") != null) {
        ChangeStatusEdit(appForm.Status.value, blnOnLoad);
    }
    else {
        ChangeStatus(appForm.Status.value);
    }

    if (sGfyx != "")
    {
        var strReqControl = openXMLHTTP("/slxt/XSZDH/Khgl_Pub_XMLHTTP.aspx?Gfyx=" + escape(sGfyx), "GetControlReq");
        if (strReqControl == "") return;

        var arrReqControl = strReqControl.split(',');
        for (var i = 0; i < arrReqControl.length; i++)
        {
            if (document.getElementById(arrReqControl[i]) != null && arrReqControl[i] != "Gzfm1" && arrReqControl[i] != "MediaName")
            {
                SetControlReq(eval('appForm.' +arrReqControl[i]), true);
            }

            if (document.getElementById(arrReqControl[i]) != null && arrReqControl[i] == "Gzfm1") {
                blnGzfmReq = true;
                document.getElementById(arrReqControl[i]).parentNode.previousSibling.style.fontWeight = "bold";
                document.getElementById(arrReqControl[i]).parentNode.previousSibling.style.color = "#990000";
            }

            if (document.getElementById("MainMediaName") != null && document.getElementById("SubMediaName") != null && arrReqControl[i] == "MediaName") {
                SetControlReq(appForm.MainMediaName, true);
                SetControlReq(appForm.SubMediaName, true);
            }      
        }

        if (strReqControl.indexOf("UserName") != -1)
        {
            SetControlReq(appForm.appForm_UserName, true);
        }

        if (strReqControl.indexOf("Gjxw") != -1)
        {
            document.getElementById("tdGjxw").className = "req";
            blnGjxwReq = true;
        }

        if (strReqControl.indexOf("YxYeTai") != -1)
        {
            blnYxYeTai = true;
        }

        if (strReqControl.indexOf("YxFangXing") != -1)
        {
            blnYxFangXing = true;
        }

        if (strReqControl.indexOf("YxArea") != -1)
        {            
            blnYxArea = true;
        }

        if (strReqControl.indexOf("Lxfs") != -1)
        {
            document.getElementById("tdLxfs").className = "req";
            blnLxfs = true;
        }

        var tblDynamicTable = document.getElementById("_DynamicTable");
        var tdDynamicTable = tblDynamicTable.getElementsByTagName("TD");
        for (var i = 0; i < tdDynamicTable.length; i++)
        {
            if (tdDynamicTable[i].name === "td_Ytxq")
            {
                if (blnYxYeTai)
                {
                    tdDynamicTable[i].className = "req";
                }
                else
                {
                    tdDynamicTable[i].className = "";
                }
            }

            if (tdDynamicTable[i].name === "td_Yxfx")
            {
                if (blnYxFangXing)
                {
                    tdDynamicTable[i].className = "req";
                }
                else
                {
                    tdDynamicTable[i].className = "";
                }
            }

            if (tdDynamicTable[i].name === "td_Xqmj")
            {
                if (blnYxArea)
                {
                    tdDynamicTable[i].className = "req";
                }
                else
                {
                    tdDynamicTable[i].className = "";
                }
            }
        }

        if (appForm.Status.value != "丢失") {
            SetControlReq(appForm.Status, true);
            SetControlReq(appForm.DsType, false);
            SetControlReq(appForm.DsReason, false);
            SetControlReq(appForm.DsRemark, false);       
        }
    }
}

//效验email格式
function checkEmail(sEmail)
{
    if (sEmail == "") return true;
    var oExp = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
    if (!oExp.test(appForm.Email.value))
    {
        return false;
    }
    else
    {
        return true;
    }
}

//改变媒体大类
function onChangeMt(strMedia)
{
    if (strMedia == "") 
    {
        RefreshDLLItem("SubMediaName", "<items><item text='' value = ''></item></items>");
    }
    var strXML = "";

    var strXML = openXMLHTTP("/slxt/XSZDH/Khgl_Pub_XMLHTTP.aspx?FjName=" + escape(strMedia) , "GET_MtZlXML");
    if (strXML != "")
    {
        RefreshDLLItem("SubMediaName", strXML);
    }
}

//修改客户状态
function CstTypeChanged(CstType)
{
    var strCstName;
    if (CstType == "个人")
    {
        if (document.getElementById("__CstName") != null)
        {
            strCstName = unescape(__CstName.value);
            if (strCstName != "")
            {
                var strFirstName = strCstName.substring(0, 1);
                var strLastName = strCstName.substr(1);
                var sCode = strFirstName.charCodeAt();
                if ((sCode >= 65 && sCode <= 90) || sCode >= 97 && sCode <= 122 || !isNaN(strFirstName)) //英文或数字
                {
                    document.getElementById("FirstName").value = strCstName;                    
                }
                else
                {
                    document.getElementById("FirstName").value = strFirstName;
                    document.getElementById("LastName").value = strLastName;
                }
            }
        }
    }
    else
    {
        if (document.getElementById("__CstName") != null)
        {
            strCstName = unescape(__CstName.value);
            appForm.CstName.value = __CstName.value;
        }
    }
}


//根据数据库值对多选框进行控制
function SetControlValue(obj, sValue)
{
    if (sValue == "") return;
    var sArr, intLen;
    sArr = sValue.split(";");
    intLen = document.all(obj).length;

    for (var i = 0; i < sArr.length; i++)
    {
        for (var j = 0; j < intLen; j++)
        {
            if (document.all(obj)[j].parentElement.innerText == sArr[i])
            {
                document.all(obj)[j].checked = true;
            }
        }
    }
}


//是否能够编辑“客户姓”、“名”、“ 公司姓名”
function CanEditCstName()
{
    if (appForm.oid.value != "")
    {
        //在整个集团范围内有“未关闭”的预约单或“激活”的交易
        var strRtn = openXMLHTTP("/slxt/XSZDH/Khgl_Pub_XMLHTTP.aspx?CstGUID=" + appForm.oid.value, "CHECKBOOKINGTRADE");
        if (strRtn == "FAIL")
        {
            if (document.getElementById("FirstName") == null)
            {               
                appForm.CstName.setReadOnly(true);
            }
            else
            {
                appForm.FirstName.setReadOnly(true);
                appForm.LastName.setReadOnly(true);
            }
        }
    }
}

//增加行数据
function AddXqRow(numvalue) {

    blnFormChanged = true;
    var r1 = _DynamicTable.insertRow();
    var t1, t2, t3, t4, t5, t6, t7, t8, t9, sCol;

    r1.style.height = "25";
    t1 = r1.insertCell();
    t1.innerHTML = '需求业态';
    t1.name="td_Ytxq"
    t2 = r1.insertCell();
    if (blnYxYeTai)
    {
        t1.className = "req";
    }  
    t2.innerHTML = '<span class="selectBox" typename="mapType" name="XqYt" id="XqYt" value="" runat=server > ' + Form1.txtXqYt.value + '</span>'
    t3 = r1.insertCell();

    t4 = r1.insertCell();
    t4.innerHTML = '需求户型';
    t4.name = "td_Yxfx"
    t5 = r1.insertCell();
    if (blnYxFangXing)
    {
        t4.className = "req";
    }
    t5.innerHTML = '<span class="selectBox" typename="mapType" name="XqHx" id="XqHx" value="" runat=server > ' + Form1.txtXqHx.value + '</span>'
    t6 = r1.insertCell();

    t7 = r1.insertCell();
    t7.innerHTML = '需求面积';
    t7.name = "td_Xqmj"
    t8 = r1.insertCell();
    if (blnYxArea)
    {
        t7.className = "req";
    }
    sCol = '<table style="FONT-SIZE: 9pt" height="100%" cellSpacing="0" cellPadding="0" width="100%" border=0><tr><td><span class="selectBox" typename="mapType" name="XqMj" id="XqMj" value="" runat=server > ' + Form1.txtXqMj.value + '</span></td>'
    if (numvalue == 1)
    {
        sCol += "<td width='20' align='center'><img id='imgadd' style='cursor:hand' src='/Slxt/images/gif_48_002.gif' align='middle' onclick='AddXqRow(0)' alt='新增记录'></td></tr></table>";
    }
    else
    {
        sCol += "<td width='20' align='center'><img id='imgdel' style='cursor:hand' src='/Slxt/images/16_close.gif' align='middle' onclick='DelXqRow(this);' alt='删除记录'></td></tr></table>";
    }
    t8.innerHTML = sCol;
}
//删除行
function DelXqRow(obj)
{
    blnFormChanged = true;
    obj.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.removeNode(true);
}

//对业态需求赋值
function SetXqRowValue()
{
    var arrAll, arrXqYt, arrXqHx, arrXqMj, j;
    if (Form1.txtXqValue.value == "") return;

    arrAll = Form1.txtXqValue.value.split("^");
    if (arrAll.length > 0)
    {
        arrXqYt = arrAll[0].split(",");
        arrXqHx = arrAll[1].split(",");
        arrXqMj = arrAll[2].split(",");
    }

    //增加多少行
    for (var i = 0; i < arrXqYt.length - 1; i++)
    {
        AddXqRow(0);
    }

    for (var i = 0; i < arrXqYt.length; i++)
    {
        j = i * 2 + 1;
        document.all("XqYt")[j].value = arrXqYt[i];
        document.all("XqHx")[j].value = arrXqHx[i];
        document.all("XqMj")[j].value = arrXqMj[i];

        document.all("XqYt")[j].returnValue = arrXqYt[i];
        document.all("XqHx")[j].returnValue = arrXqHx[i];
        document.all("XqMj")[j].returnValue = arrXqMj[i];
    }
}

//客户状态改变-新增机会页面
function ChangeStatus(sValue)
{
    if (sValue == "") return;
    // 按照回访追访参数设置“下次跟进日期”是否必填
    setNextDateIsReq(sValue);
    
    if (sValue == "看房")
    {
        SetControlReq(appForm.MainMediaName, true);
        SetControlReq(appForm.SubMediaName, true);
    }
    else
    {
        SetControlReq(appForm.MainMediaName, false);
        SetControlReq(appForm.SubMediaName, false);
    }
    setNextContentValue();
}

//客户状态改变-修改机会页面
function ChangeStatusEdit(sValue,blnOnLoad)
{
    if (sValue == "") return;
    if (blnOnLoad == undefined || blnOnLoad == null)
    {
        blnOnLoad = false;
    }
    
    if (blnOnLoad)
    {
        if (sValue == "问询" || sValue == "看房")
        {
            appForm.Status.setReadOnly(false);
        }
        else
        {
            appForm.Status.setReadOnly(true);            
        } 
    }
    
    if (sValue == "丢失")
    {
        SetControlReq(appForm.Status, false);
        SetControlReq(appForm.DsType, true);
        SetControlReq(appForm.DsReason, true);        
        
        SetControlReq(appForm.NextContent, false);
        SetControlReq(appForm.NextDate, false);       
        document.all("Sec7").style.display = "";
    }
    else
    {
        SetControlReq(appForm.Status, true);
        SetControlReq(appForm.DsType, false);
        SetControlReq(appForm.DsReason, false);
        SetControlReq(appForm.DsRemark, false);
        document.all("Sec7").style.display = "none";
        // 按照回访追访参数设置“下次跟进日期”是否必填
        setNextDateIsReq(sValue);
    }

    if (sValue == "问询" || sValue == "丢失")
    {
        SetControlReq(appForm.MainMediaName, false);
        SetControlReq(appForm.SubMediaName, false);
    }
    else
    {
        SetControlReq(appForm.MainMediaName, true);
        SetControlReq(appForm.SubMediaName, true);
    }
    
    setNextContentValue();
}

// 按照回访追访参数设置“下次跟进日期”是否必填
// sStatus:客户状态
function setNextDateIsReq(sStatus) {
    // 如果当前机会客户状态已经高于或者等于设置了回访追访时间的最高客户状态，将“下次跟进日期”设置为非必填
    // 默认非必填
    SetControlReq(appForm.NextDate, false);
    // 没有设置回访追访参数，则退出函数
    if (!sGlobalMaxStatus) return;
    var arrStatus = [{ status: "签约", no: 5 }, { status: "认购", no: 4 }, { status: "预约", no: 3 }, { status: "看房", no: 2 }, { status: "问询", no: 1}];
    for (var i = 0; i < arrStatus.length; i++) {
        if (arrStatus[i].status === sStatus) {
            if (arrStatus[i].no < sGlobalMaxStatus) {
                SetControlReq(appForm.NextDate, true);
                break;
            }
        }
    }
}

//将所有控件设置为非必填
function setAllControlReqFalse()
{
    
    blnGjxwReq = false; //关键行为是否必填
    blnYxYeTai = false; //需求业态是否必填
    blnYxFangXing = false; //需求户型是否必填
    blnYxArea = false; //需求面积是否必填
    blnLxfs = false; //联系方式是否必填
    
    SetControlReq(appForm.Email, false);
    SetControlReq(appForm.IsYz, false);

    SetControlReq(appForm.IsMainJcz, false);
    SetControlReq(appForm.FromArea, false);
    SetControlReq(appForm.HomeArea, false);
    SetControlReq(appForm.WorkArea, false);
    SetControlReq(appForm.Jzyt, false);
    SetControlReq(appForm.Family, false);
    SetControlReq(appForm.ZnNum, false);
    SetControlReq(appForm.ZyNum, false);
    SetControlReq(appForm.Work, false);
    if (document.getElementById("CardID") != null)
    {
        SetControlReq(appForm.CardID, false);
    }
    if (document.getElementById("BirthDate") != null)
    {
        SetControlReq(appForm.BirthDate, false);
    }
    if (document.getElementById("Address") != null)
    {
        SetControlReq(appForm.Address, false);
    }
    if (document.getElementById("PostCode") != null)
    {
        SetControlReq(appForm.PostCode, false);
    }
    if (document.getElementById("CstType") != null)
    {
        SetControlReq(appForm.CstType, false);
    }
    if (document.getElementById("OppSource") != null)
    {
        SetControlReq(appForm.OppSource, false);
    }

    SetControlReq(appForm.MainMediaName, false);
    SetControlReq(appForm.SubMediaName, false);
    SetControlReq(appForm.appForm_UserName, false);
    SetControlReq(appForm.Gfys, false);
    SetControlReq(appForm.Gfyt, false);
    SetControlReq(appForm.Xqts, false);

    SetControlReq(appForm.Gzqs, false);
    SetControlReq(appForm.Gzjg, false);
    SetControlReq(appForm.Competitor, false);
    SetControlReq(appForm.Gzfm1, false);

    SetControlReq(appForm.Gzqs, false);
    SetControlReq(appForm.Gzjg, false);
    SetControlReq(appForm.Competitor, false);
    SetControlReq(appForm.Gzfm1, false);

    SetControlReq(appForm.DsRemark, false);
    SetControlReq(appForm.Gjr, false);
    SetControlReq(appForm.Competitor, false);
    SetControlReq(appForm.DfNum, false);
    SetControlReq(appForm.GjContent, false);
    SetControlReq(appForm.NextDate, false);
    SetControlReq(appForm.NextContent, false);
    SetControlReq(appForm.Remark, false);
    SetControlReq(appForm.EduLevel, false);
    SetControlReq(appForm.HaveBM, false);
    
    var tblDynamicTable = document.getElementById("_DynamicTable");
    var tdDynamicTable = tblDynamicTable.getElementsByTagName("TD");
    for (var i = 0; i < tdDynamicTable.length; i++) {
        if (tdDynamicTable[i].name === "td_Ytxq") {
            if (blnYxYeTai) {
                tdDynamicTable[i].className = "req";
            }
            else {
                tdDynamicTable[i].className = "";
            }
        }

        if (tdDynamicTable[i].name === "td_Yxfx") {
            if (blnYxFangXing) {
                tdDynamicTable[i].className = "req";
            }
            else {
                tdDynamicTable[i].className = "";
            }
        }

        if (tdDynamicTable[i].name === "td_Xqmj") {
            if (blnYxArea) {
                tdDynamicTable[i].className = "req";
            }
            else {
                tdDynamicTable[i].className = "";
            }
        }
    }
    
    document.getElementById("tdLxfs").className = "";
    document.getElementById("tdGjxw").className = "";

    try {
        if (__from.value == "new") {
            SetControlReq(appForm.appForm_UserName, true);
        }
    }
    catch (ex) {
    }
    
}

//选择置业顾问
function SelectUser()
{
    // 返回 arrTeamUser 为用户信息数组：Array{UserGUID,UserName,UserCode}
    var arrTeamUser = _selectTeamUser(__ProjGUID.value, "0", "0", "0101"); // 可选参数为项目GUID，弹出窗口会定位到该项目。
    if (arrTeamUser == null || arrTeamUser == undefined) return;
    appForm.UserGUID.value = arrTeamUser[0];
    appForm.appForm_UserName.setValue(arrTeamUser[1]);
}

//得到多选框的值
function GetControlValue(obj) 
{
    var sValue, intLen;
    intLen = document.all(obj).length;

    sValue = "";
    for (var j = 0; j < intLen; j++) 
    {
        if (document.all(obj)[j].checked) 
        {
            if (sValue == "") 
            {
                sValue = document.all(obj)[j].parentElement.innerText;
            }
            else 
            {
                sValue = sValue + ";" + document.all(obj)[j].parentElement.innerText;
            }
        }
    }
    return sValue;
}

//设置跟进记录列表
function SetGjls()
{
    var oDynamicTableGjls = document.getElementById("DynamicTableGjls");
    if (document.all("txtGjls").value != "")
    {
        var strXml = "<?xml version=\"1.0\" encoding=\"GB2312\" ?><content>" + document.all("txtGjls").value + "</content>";
        var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = false;
        xmlDoc.loadXML(strXml);
        var arrTr = xmlDoc.selectNodes("/content/tr");
        for (var i = arrTr.length -1,j=1; i >= 0; i--,j++)
        {
            //序号
            var oTr = oDynamicTableGjls.insertRow();
            var oTd = oTr.insertCell();
            oTd.style.textAlign = "center";
            oTd.innerHTML = j;

            //备注
            oTd = oTr.insertCell();
            oTd.style.textAlign = "left";
            oTd.innerHTML = arrTr[i].childNodes(0).text == "" ? "&nbsp;" : arrTr[i].childNodes(0).text;

            //联系状态							
            oTd = oTr.insertCell();
            oTd.innerHTML = arrTr[i].childNodes(1).text;

            //联系日期
            oTd = oTr.insertCell();
            oTd.style.textAlign = "center";
            oTd.innerHTML = arrTr[i].childNodes(2).text;

            //客户状态
            oTd = oTr.insertCell();
            oTd.style.textAlign = "center";
            oTd.innerHTML = arrTr[i].childNodes(3).text;

            //购房意向							
            oTd = oTr.insertCell();
            oTd.style.textAlign = "center";
            oTd.innerHTML = arrTr[i].childNodes(4).text;

            //跟进方式							
            oTd = oTr.insertCell();
            oTd.style.textAlign = "center";
            oTd.innerHTML = arrTr[i].childNodes(5).text;

            //跟进内容							
            oTd = oTr.insertCell();
            oTd.innerHTML = arrTr[i].childNodes(6).text == "" ? "&nbsp;" : arrTr[i].childNodes(6).text;

            //跟进人							
            oTd = oTr.insertCell();
            oTd.innerHTML = arrTr[i].childNodes(7).text;
        }
        xmlDoc = null;
    }
    else
    {
        document.getElementById("Sec8").style.display = "none";
    }
}

//修改身份证
function CardIDChanged(blnFlag)
{
    var sCardId = appForm.CardID.value;
    if (sCardId == "") return true;
    // 非身份证直接返回true
    if (sCardId.length != 15 && sCardId.length != 18) return true;
    
    var arrRt = checkGetCardIDInfo(sCardId);
    if (arrRt[0] != "1")
    {        
        appForm.BirthDate.setValue("");
        appForm.NativePlace.value = "";
        if (blnFlag)
        {
            alert("您输入的身份证号不正确，请重新输入！");
            appForm.CardID.focus();
        }
        return false;
    }
    else
    {
        if (arrRt[1] != undefined && arrRt[1] != null)
        {
            appForm.BirthDate.setValue(arrRt[1]);
        }
        if (arrRt[2] != undefined && arrRt[2] != null)
        {
            if (document.getElementById("Gender") != null)
            {
                appForm.Gender.setValue(arrRt[2]);
            }
        }
        if (arrRt[3] != undefined && arrRt[3] != null)
        {
            appForm.NativePlace.value =arrRt[3];
        }
        return true;
    }
    return false;
}

function getP_CustomerXml()
{
    var sDataXML = "";
    var sCstGUID = "";
    var sCstName = "";
    var sCompanyPhone = "";
    if (document.getElementById("__oid") != null)
    {
        sCstGUID = __oid.value;
    }
    sDataXML += "<p_Customer keyname=\"CstGUID\" keyvalue='" + sCstGUID + "'>";
    if (document.getElementById("FirstName") == null)
    {
        sCstName = appForm.CstName.value; //公司
        sCompanyPhone = appForm.OfficeTel.value;
        sDataXML += "<LastName><![CDATA[" + sCstName + "]]></LastName>";
    }
    else
    {
        sCstName = appForm.FirstName.value + appForm.LastName.value; //个人
        sDataXML += "<FirstName><![CDATA[" + appForm.FirstName.value + "]]></FirstName>";
        sDataXML += "<LastName><![CDATA[" + appForm.LastName.value + "]]></LastName>";
    }

    if (document.getElementById("CardID") != null)
    {
        sDataXML += "<CardID><![CDATA[" + appForm.CardID.value + "]]></CardID>";
        if (appForm.CardID.value == "")
        {
            sDataXML += "<CardType></CardType>";
        }
        else
        {
            sDataXML += "<CardType>身份证号码</CardType>";
        }
    }

    sDataXML += "<CstName><![CDATA[" + sCstName + "]]></CstName>";
    sDataXML += "<OfficeTel><![CDATA[" + appForm.OfficeTel.value + "]]></OfficeTel>";
    sDataXML += "<HomeTel><![CDATA[" + appForm.HomeTel.value + "]]></HomeTel>";
    sDataXML += "<Email><![CDATA[" + appForm.Email.value + "]]></Email>";
    sDataXML += "<MobileTel>" + appForm.MobileTel.value + "</MobileTel>";
    if (document.getElementById("__CstType") != null)
    {
        sDataXML += "<CstType>" + unescape(__CstType.value) + "</CstType>";
    }
    else
    {
        sDataXML += "<CstType>" + appForm.CstType.value + "</CstType>";
    }

    if (document.getElementById("PostCode") != null)
    {
        sDataXML += "<PostCode><![CDATA[" + appForm.PostCode.value + "]]></PostCode>";
    }

    if (document.getElementById("Address") != null)
    {
        sDataXML += "<Address><![CDATA[" + appForm.Address.value + "]]></Address>";
    }

    if (document.getElementById("NativePlace") != null)
    {
        sDataXML += "<NativePlace>" + appForm.NativePlace.value + "</NativePlace>";
    }

    if (document.getElementById("Gender") != null)
    {
        sDataXML += "<Gender>" + appForm.Gender.value + "</Gender>";
    }
    sDataXML += "<Fax><![CDATA[" + appForm.Fax.value + "]]></Fax>";
    sDataXML += "<ModifyOn>[当前时间]</ModifyOn>";
    sDataXML += "<ModifyBy>" + appForm.Gjr.value + "</ModifyBy>";
    if (document.getElementById("__CstType") != null)
    {
        if (unescape(__CstType.value) == "公司")
        {
            sDataXML += "<CompanyPhone>" + appForm.OfficeTel.value + "</CompanyPhone>";
        }
    }

    var sLxfs = GetControlValue("chkLxfs");

    if (sLxfs.indexOf("短信") != -1)
    {
        sDataXML += "<IsReceiveSMS>1</IsReceiveSMS>";
    }
    else
    {
        sDataXML += "<IsReceiveSMS>0</IsReceiveSMS>";
    }
    if (sCstGUID == "")
    {
        sDataXML += "<CreatedOn>[当前时间]</CreatedOn>";
        sDataXML += "<CreatedBy>" + appForm.CreatedBy.value + "</CreatedBy>";
        sDataXML += "<CreatedByGUID>" + appForm.CreatedByGUID.value + "</CreatedByGUID>";
    }

    if (document.getElementById("BirthDate") != null)
    {
        sDataXML += "<BirthDate>" + appForm.BirthDate.value + "</BirthDate>";
    }
    sDataXML += "</p_Customer>";
    return sDataXML;
}


function getP_CstAttributeXml()
{
    var sDataXML = "";
    var sCstGUID = "";
    if (document.getElementById("__oid") != null)
    {
        sCstGUID = document.getElementById("__oid").value;
        if (sCstGUID == "")
        {
            sCstGUID = "[CstGUID]"
        }
    }
    sDataXML = "<P_CstAttribute keyname=\"CstGUID\" keyvalue='" + sCstGUID + "'>";
    sDataXML += "<WorkArea>" + appForm.WorkArea.value + "</WorkArea>";
    sDataXML += "<HomeArea>" + appForm.HomeArea.value + "</HomeArea>";
    sDataXML += "<EduLevel><![CDATA[" + appForm.EduLevel.value + "]]></EduLevel>";
    sDataXML += "<Family><![CDATA[" + appForm.Family.value + "]]></Family>";
    sDataXML += "<Work><![CDATA[" + appForm.Work.value + "]]></Work>";
    sDataXML += "<BuyersUse><![CDATA[" + appForm.Gfyt.value + "]]></BuyersUse>";
    sDataXML += "<FromArea>" + appForm.FromArea.value + "</FromArea>";
    sDataXML += "<Jzyt>" + appForm.Jzyt.value + "</Jzyt>";
    sDataXML += "<ZnNum><![CDATA[" + appForm.ZnNum.value + "]]></ZnNum>";
    sDataXML += "<ZyNum><![CDATA[" + appForm.ZyNum.value + "]]></ZyNum>";
    sDataXML += "<HaveBM><![CDATA[" + appForm.HaveBM.value + "]]></HaveBM>";
    sDataXML += "<IsMainJcz><![CDATA[" + appForm.IsMainJcz.value + "]]></IsMainJcz>";
    sDataXML += "<Lxfs><![CDATA[" + GetControlValue("chkLxfs") + "]]></Lxfs>";
    sDataXML += "</P_CstAttribute>";
    return sDataXML;
}

function getP_CstLog()
{
    var sDataXML = "";
    var sValue, sCstAttachGUID;
    if (document.getElementById("__oid") == null)
    {
        return "";
    }

    if (document.getElementById("FirstName") != null && document.getElementById("LastName") != null)
    {
        if ((appForm.FirstName.value != appForm.appForm_OldFirstName.value || appForm.LastName.value != appForm.appForm_OldLastName.value) && appForm.appForm_OldLastName.value != "") {
            sDataXML += "<p_CstLog keyname=\"CstLogGUID\" keyvalue=\"\">";
            sDataXML += "<CstGUID>[CstGUID]</CstGUID>";
            sDataXML += "<ChangeFieldchn>客户名称</ChangeFieldchn>";
            sDataXML += "<ChangeField>CstName</ChangeField>";
            sDataXML += "<ChangeBefore><![CDATA[" + appForm.appForm_OldFirstName.value + appForm.appForm_OldLastName.value + "]]></ChangeBefore>";
            sDataXML += "<ChangeAfter><![CDATA[" + appForm.FirstName.value + appForm.LastName.value + "]]></ChangeAfter>";
            sDataXML += "<ModifyOn>[当前时间]</ModifyOn>";
            sDataXML += "<ModifyBy>" + appForm.Gjr.value + "</ModifyBy>";
            sDataXML += "<ModifyByGUID>" + appForm.GjrGUID.value + "</ModifyByGUID>";
            sDataXML += "</p_CstLog>"
        }
    }
    else if (document.getElementById("CstName") != null)
    {
        if (appForm.CstName.value != appForm.appForm_OldCstName.value && appForm.appForm_OldCstName.value != "") {
            sDataXML += "<p_CstLog keyname=\"CstLogGUID\" keyvalue=\"\">";
            sDataXML += "<CstGUID>[CstGUID]</CstGUID>";
            sDataXML += "<ChangeFieldchn>客户名称</ChangeFieldchn>";
            sDataXML += "<ChangeField>CstName</ChangeField>";
            sDataXML += "<ChangeBefore><![CDATA[" + appForm.appForm_OldCstName.value + "]]></ChangeBefore>";
            sDataXML += "<ChangeAfter><![CDATA[" + appForm.CstName.value + "]]></ChangeAfter>";
            sDataXML += "<ModifyOn>[当前时间]</ModifyOn>";
            sDataXML += "<ModifyBy>" + appForm.Gjr.value + "</ModifyBy>";
            sDataXML += "<ModifyByGUID>" + appForm.GjrGUID.value + "</ModifyByGUID>";
            sDataXML += "</p_CstLog>"
        }
    }

    if (document.getElementById("MobileTel") != null)
    {
        if (appForm.MobileTel.value != appForm.appForm_OldMobileTel.value && appForm.appForm_OldMobileTel.value != "") {
            sDataXML += "<p_CstLog keyname=\"CstLogGUID\" keyvalue=\"\">";
            sDataXML += "<CstGUID>[CstGUID]</CstGUID>";
            sDataXML += "<ChangeFieldchn>移动电话</ChangeFieldchn>";
            sDataXML += "<ChangeField>MobileTel</ChangeField>";
            sDataXML += "<ChangeBefore><![CDATA[" + appForm.appForm_OldMobileTel.value + "]]></ChangeBefore>";
            sDataXML += "<ChangeAfter><![CDATA[" + appForm.MobileTel.value + "]]></ChangeAfter>";
            sDataXML += "<ModifyOn>[当前时间]</ModifyOn>";
            sDataXML += "<ModifyBy>" + appForm.Gjr.value + "</ModifyBy>";
            sDataXML += "<ModifyByGUID>" + appForm.GjrGUID.value + "</ModifyByGUID>";
            sDataXML += "</p_CstLog>"
        }
    }

    if (document.getElementById("OfficeTel") != null)
    {
        if (appForm.OfficeTel.value != appForm.appForm_OldOfficeTel.value && appForm.appForm_OldOfficeTel.value != "")
        {
            sDataXML += "<p_CstLog keyname=\"CstLogGUID\" keyvalue=\"\">";
            sDataXML += "<CstGUID>[CstGUID]</CstGUID>";
            sDataXML += "<ChangeFieldchn>办公电话</ChangeFieldchn>";
            sDataXML += "<ChangeField>OfficeTel</ChangeField>";
            sDataXML += "<ChangeBefore><![CDATA[" + appForm.appForm_OldOfficeTel.value + "]]></ChangeBefore>";
            sDataXML += "<ChangeAfter><![CDATA[" + appForm.OfficeTel.value + "]]></ChangeAfter>";
            sDataXML += "<ModifyOn>[当前时间]</ModifyOn>";
            sDataXML += "<ModifyBy>" + appForm.Gjr.value + "</ModifyBy>";
            sDataXML += "<ModifyByGUID>" + appForm.GjrGUID.value + "</ModifyByGUID>";
            sDataXML += "</p_CstLog>"
        }
    }

    if (document.getElementById("HomeTel") != null)
    {
        if (appForm.HomeTel.value != appForm.appForm_OldHomeTel.value && appForm.appForm_OldHomeTel.value != "")
        {
            sDataXML += "<p_CstLog keyname=\"CstLogGUID\" keyvalue=\"\">";
            sDataXML += "<CstGUID>[CstGUID]</CstGUID>";
            sDataXML += "<ChangeFieldchn>住宅电话</ChangeFieldchn>";
            sDataXML += "<ChangeField>HomeTel</ChangeField>";
            sDataXML += "<ChangeBefore><![CDATA[" + appForm.appForm_OldHomeTel.value + "]]></ChangeBefore>";
            sDataXML += "<ChangeAfter><![CDATA[" + appForm.HomeTel.value + "]]></ChangeAfter>";
            sDataXML += "<ModifyOn>[当前时间]</ModifyOn>";
            sDataXML += "<ModifyBy>" + appForm.Gjr.value + "</ModifyBy>";
            sDataXML += "<ModifyByGUID>" + appForm.GjrGUID.value + "</ModifyByGUID>";
            sDataXML += "</p_CstLog>"
        }
    }

    if (document.getElementById("Fax") != null)
    {
        if (appForm.Fax.value != appForm.appForm_OldFax.value && appForm.appForm_OldFax.value != "")
        {
            sDataXML += "<p_CstLog keyname=\"CstLogGUID\" keyvalue=\"\">";
            sDataXML += "<CstGUID>[CstGUID]</CstGUID>";
            sDataXML += "<ChangeFieldchn>传真</ChangeFieldchn>";
            sDataXML += "<ChangeField>Fax</ChangeField>";
            sDataXML += "<ChangeBefore><![CDATA[" + appForm.appForm_OldFax.value + "]]></ChangeBefore>";
            sDataXML += "<ChangeAfter><![CDATA[" + appForm.Fax.value + "]]></ChangeAfter>";
            sDataXML += "<ModifyOn>[当前时间]</ModifyOn>";
            sDataXML += "<ModifyBy>" + appForm.Gjr.value + "</ModifyBy>";
            sDataXML += "<ModifyByGUID>" + appForm.GjrGUID.value + "</ModifyByGUID>";
            sDataXML += "</p_CstLog>"
        }
    }

    if (document.getElementById("Address") != null)
    {
        if (appForm.Address.value != appForm.appForm_OldAddress.value && appForm.appForm_OldAddress.value != "")
        {
            sDataXML += "<p_CstLog keyname=\"CstLogGUID\" keyvalue=\"\">";
            sDataXML += "<CstGUID>[CstGUID]</CstGUID>";
            sDataXML += "<ChangeFieldchn>通讯地址</ChangeFieldchn>";
            sDataXML += "<ChangeField>Address</ChangeField>";
            sDataXML += "<ChangeBefore><![CDATA[" + appForm.appForm_OldAddress.value + "]]></ChangeBefore>";
            sDataXML += "<ChangeAfter><![CDATA[" + appForm.Address.value + "]]></ChangeAfter>";
            sDataXML += "<ModifyOn>[当前时间]</ModifyOn>";
            sDataXML += "<ModifyBy>" + appForm.Gjr.value + "</ModifyBy>";
            sDataXML += "<ModifyByGUID>" + appForm.GjrGUID.value + "</ModifyByGUID>";
            sDataXML += "</p_CstLog>"
        }
    }

    if (document.getElementById("CardID") != null)
    {
        if (appForm.CardID.value != appForm.appForm_OldCardID.value && appForm.appForm_OldCardID.value != "")
        {
            sDataXML += "<p_CstLog keyname=\"CstLogGUID\" keyvalue=\"\">";
            sDataXML += "<CstGUID>[CstGUID]</CstGUID>";
            sDataXML += "<ChangeFieldchn>证件号码</ChangeFieldchn>";
            sDataXML += "<ChangeField>CardID</ChangeField>";
            sDataXML += "<ChangeBefore><![CDATA[" + appForm.appForm_OldCardID.value + "]]></ChangeBefore>";
            sDataXML += "<ChangeAfter><![CDATA[" + appForm.CardID.value + "]]></ChangeAfter>";
            sDataXML += "<ModifyOn>[当前时间]</ModifyOn>";
            sDataXML += "<ModifyBy>" + appForm.Gjr.value + "</ModifyBy>";
            sDataXML += "<ModifyByGUID>" + appForm.GjrGUID.value + "</ModifyByGUID>";
            sDataXML += "</p_CstLog>"
        }
    }
    return sDataXML;
}


function getP_CstAttachXml()
{
    var sDataXML = "";
    var sValue, sCstAttachGUID;
    sDataXML = "<p_CstAttach keyname=\"CstAttachGUID\" keyvalue=\"\">";
    sDataXML += "<BUGUID>[当前公司]</BUGUID>";
    sDataXML += "<CstGUID>[CstGUID]</CstGUID>";
    sDataXML += "<ProjGUID>" + __ProjGUID.value + "</ProjGUID>";
    sDataXML += "<UserGUID>" + appForm.UserGUID.value + "</UserGUID>";
    sDataXML += "</p_CstAttach>";
    return sDataXML;
}


function getS_Opportunity(IsEdit)
{
    if (IsEdit == null || IsEdit == undefined)
    {
        IsEdit = false;
    }
    var sDataXML = "";
    var sOppGUID = "";
    if (document.getElementById("OppGUID") != null)
    {
        sOppGUID = appForm.OppGUID.value;
    }
    sDataXML = "<s_Opportunity keyname=\"OppGUID\" keyvalue='" + sOppGUID + "'>";
    sDataXML += "<BUGUID>[当前公司]</BUGUID>";
    sDataXML += "<ProjGUID>" + __ProjGUID.value + "</ProjGUID>";
    if (document.getElementById("VisitStatusLd") != null)
    {
        if (document.getElementById("VisitStatusLd").checked)
        {
            sDataXML += "<OppSource>来电</OppSource>";
        }
        else
        {
            sDataXML += "<OppSource>来访</OppSource>";
        }
    }
    sDataXML += "<CstGUID>[CstGUID]</CstGUID>";
    sDataXML += "<Status>" + appForm.Status.value + "</Status>";
    if (!IsEdit)
    {
        sDataXML += "<CreatedOn>[当前时间]</CreatedOn>";
        sDataXML += "<CreatedBy>" + appForm.CreatedBy.value + "</CreatedBy>";
        sDataXML += "<CreatedByGUID>" + appForm.CreatedByGUID.value + "</CreatedByGUID>";
        sDataXML += "<IsCreatorUse>1</IsCreatorUse>";
    }
    sDataXML += "<Competitor>" + appForm.Competitor.value + "</Competitor>";
    sDataXML += "<ModifyOn>[当前时间]</ModifyOn>";
    sDataXML += "<ModifyBy>" + appForm.Gjr.value + "</ModifyBy>";
    sDataXML += "<MainMediaName>" + appForm.MainMediaName.value + "</MainMediaName>";
    sDataXML += "<SubMediaName>" + appForm.SubMediaName.value + "</SubMediaName>";
    sDataXML += "<Gfys>" + appForm.Gfys.value + "</Gfys>";
    sDataXML += "<Gfyt><![CDATA[" + appForm.Gfyt.value + "]]></Gfyt>";
    sDataXML += "<Xqts>" + appForm.Xqts.value + "</Xqts>";
    sDataXML += "<Gzqs>" + appForm.Gzqs.value + "</Gzqs>";
    sDataXML += "<Gzjg>" + appForm.Gzjg.value + "</Gzjg>";
    sDataXML += "<Gzfm1>" + appForm.Gzfm1.value + "</Gzfm1>";
    sDataXML += "<Gzfm2>" + appForm.Gzfm2.value + "</Gzfm2>";
    sDataXML += "<Gzfm3>" + appForm.Gzfm3.value + "</Gzfm3>";
    if (document.getElementById("DsType") != null)
    {
        sDataXML += "<DsType>" + appForm.DsType.value + "</DsType>";
    }
    if (document.getElementById("DsReason") != null)
    {
        sDataXML += "<DsReason>" + appForm.DsReason.value + "</DsReason>";
    }
    if (document.getElementById("DsRemark") != null)
    {
        sDataXML += "<DsRemark>" + appForm.DsRemark.value + "</DsRemark>";
    }
    sDataXML += "<UserGUID>" + appForm.UserGUID.value + "</UserGUID>";

    sDataXML += "<LastDate>[当前日期]</LastDate>";
    sDataXML += "<NextDate>" + appForm.NextDate.value + "</NextDate>";
    sDataXML += "<NextContent><![CDATA[" + appForm.NextContent.value + "]]></NextContent>";
    if (appForm.Status.value == "丢失")
    {        
        sDataXML += "<ClosedOn>[当前日期]</ClosedOn>";
    }
    sDataXML += "<Gfyx>" + appForm.Gfyx.value + "</Gfyx>";
    if (IsEdit)
    {
        if (appForm.Gjfs.value == "现场接待")
        {
            var iDfNum = parseInt(appForm.DfNum.value, 10) + 1;
            sDataXML += "<DfNum>" + iDfNum + "</DfNum>";
        }
    }
    else
    {
        if (appForm.Gjfs.value == "现场接待")
        {
            sDataXML += "<DfNum>1</DfNum>";
        }
        else
        {
            sDataXML += "<DfNum>0</DfNum>";
        }
        sDataXML += "<FpNum>0</FpNum>";

        sDataXML += "<FirstGfyx>" + appForm.Gfyx.value + "</FirstGfyx>";
    }
    
    sDataXML += "</s_Opportunity>";
    return sDataXML;
}

//得到需求业态数据
function getXqDataXML()
{
    var sDataXML = "";
    var intLen, sXqYt, sXqHx, sXqMj;
    intLen = document.all("XqYt").length;
    var j = 1;
    for (var i = 1; i < intLen; i = i + 2)
    {
        
        sXqYt = document.all("XqYt")[i].value;
        sXqHx = document.all("XqHx")[i].value;
        sXqMj = document.all("XqMj")[i].value;

        if (sXqYt == "" && sXqHx == "" && sXqMj == "")
            continue;
        sDataXML += "<s_Opp2Room keyname=\"RowGUID\" keyvalue=\"\">";
        sDataXML += "<YxYeTai>" + sXqYt + "</YxYeTai>";
        sDataXML += "<YxFangXing>" + sXqHx + "</YxFangXing>";
        sDataXML += "<YxArea>" + sXqMj + "</YxArea>";
        sDataXML += "<OppGUID>[OppGUID]</OppGUID>";
        sDataXML += "<Num>" + j + "</Num>";
        sDataXML += "</s_Opp2Room>";
        j = j + 1;
    }
    return sDataXML;
}

//效验业态数据的必填项
function validateXqData()
{
    var intLen, sXqYt, sXqHx, sXqMj;
    intLen = document.all("XqYt").length;
    for (var i = 1; i < intLen; i = i + 2)
    {
        sXqYt = document.all("XqYt")[i].value;
        sXqHx = document.all("XqHx")[i].value;
        sXqMj = document.all("XqMj")[i].value;
        if(blnYxYeTai)
        {
            if(sXqYt =="")
            {
                alert('请录入“需求业态”！');
                return false;
            }
        }
        
        if(blnYxFangXing)
        {
            if (sXqHx == "")
            {
                alert('请录入“需求户型”！');
                return false;
            }
        }

        if (blnYxArea)
        {
            if (sXqMj == "")
            {
                alert('请录入“需求面积”！');
                return false;
            }
        }

    }
    return true;
}


function getS_gjjlXML()
{
    if (appForm.Gjfs.value == "" || appForm.Gfyx.value == "" || appForm.Status.value =="")
    {
        return "";
    }
    var sDataXML = "";
    sDataXML += "<s_Opp2Gjjl keyname=\"GjjlGUID\" keyvalue=''>";
    sDataXML += "<OppGUID>[OppGUID]</OppGUID>";
    sDataXML += "<Status>" + appForm.Status.value + "</Status>";
    sDataXML += "<Gfyx>" + appForm.Gfyx.value + "</Gfyx>";
    sDataXML += "<GjDate>[当前日期]</GjDate>";       
    sDataXML += "<GjrGUID>"+ appForm.GjrGUID.value +"</GjrGUID>";
    sDataXML += "<Gjfs>" + appForm.Gjfs.value + "</Gjfs>";
    sDataXML += "<Gjxw>" + GetControlValue("chkGjxw") + "</Gjxw>";
    sDataXML += "<GjContent><![CDATA[" + appForm.GjContent.value + "]]></GjContent>";    
    sDataXML += "<NextDate>" + appForm.NextDate.value + "</NextDate>";
    sDataXML += "<NextContent><![CDATA[" + appForm.NextContent.value + "]]></NextContent>";
    sDataXML += "<Remark><![CDATA[" + appForm.Remark.value + "]]></Remark>";
    sDataXML += "</s_Opp2Gjjl>";
    return sDataXML;
}

function getS_Opp2Cst()
{
    var sDataXML = "";
    sDataXML = "<s_Opp2Cst keyname=\"Opp2CstGUID\" keyvalue=\"\">";
    sDataXML += "<OppGUID>[OppGUID]</OppGUID>";
    sDataXML += "<CstGUID>[CstGUID]</CstGUID>";
    sDataXML += "<CstNum>1</CstNum>";
    sDataXML += "</s_Opp2Cst>";
    return sDataXML;
}

function getS_Lead()
{
    if (document.getElementById("__LeadGUID") == null)
    {
        return "";
    }
    var sDataXML = "";
    sDataXML = "<s_Lead keyname=\"LeadGUID\" keyvalue='" + __LeadGUID.value + "'>";
    sDataXML += "<Status>合格</Status>";
    sDataXML += "<StatusReason>合格</StatusReason>";
    sDataXML += "<ClosedOn>[当前时间1]</ClosedOn>";
    sDataXML += "</s_Lead>";
    return sDataXML;
}
			

//返回主页
function ReturnMain()
{
    parent.isReFresh = 0;
    parent.appIframe2.location = "about:blank";
    if (blnAlreadyRh)
    {
        blnAlreadyRh = false;
        parent.appIframe.window.appGrid.frameElement.Query(__OppGUID.value);
    }
}

function window.onunload()
{
    try
        {
            if (!blnAlreadyRh)
            {
                parent.document.getElementById("div1").style.display = "";
                parent.document.getElementById("div2").style.display = "none";
                parent.document.getElementById("div3").style.display = "none";
            }
    }
    catch (e) { }
}

// 是否初始化
// 初始化时下次跟进内容按照初始化的购房意向获取
var blnIsInit = true;
//设置下次跟进内容
function setNextContentValue() {
    var sStatus = appForm.Status.value;
    var sGfyx = appForm.Gfyx.value;
    if (event.srcElement != null && event.srcElement.name == "appForm_Status")
    {
        sStatus = event.srcElement.value;
        
    }
    else if (event.srcElement != null && event.srcElement.name == "appForm_Gfyx")
    {
        sGfyx = event.srcElement.value;
    }
    
    //如果客户状态没有发生改变则不根据状态取
    if (sGlobalStatus === sStatus) {
        sStatus = "";
    }
    //如果购房意向没有发生改变则不根据意向取
    if (sGlobalGfyx === sGfyx) {
        sGfyx = "";
    }
    // 页面初始化，则默认通过购房意向为无进行下次跟进内容的初始化
    // 同步是否初始化参数为false：已初始化
    if (blnIsInit) {
        sGfyx = sGlobalGfyx;
        blnIsInit = false; 
    }

     
    //如果都没有发生改变则不取
    if (sStatus === "" && sGfyx === "") {
        return;
    } 
    var sRtn = GetNextGjInfo(sStatus, sGfyx, __ProjGUID.value);
    // 如果没有取到下次跟进的日期和内容，则退出保持不变
    if (!sRtn || sRtn == "|") {
        return;
    }
    appForm.NextDate.setValue(sRtn.split("|")[0]);
    appForm.NextContent.setValue(sRtn.split("|")[1]);
}

//保存
function saveData(IsRh)
{
    if (IsRh == undefined || IsRh == null)
    {
        IsRh = false;
    }

    if (!validate())
    {
        return false;
    }

    var sRtn;
    var o = document.getElementById("__OppGUID");
    if (o == null)
    {
        sRtn = getXmlAndSave_New();
    }
    else
    {
        sRtn = getXmlAndSave_Edit();
    }

    if (sRtn.indexOf("OK") != -1) 
    {
        if (o == null)
        {
            afterSave_New(sRtn);
        }
        else
        {
            afterSave_Edit(IsRh);
        }
        return true;
    }
    else
    {
        alert("保存失败，请联系管理员！");
        return false;
    }
}

//保存前效验
function validate()
{
    if (document.getElementById("FirstName") != null)
    {
        if (document.getElementById("FirstName").value == "")
        {
            alert("请录入“客户姓”！");
            document.getElementById("FirstName").focus();
            return false;
        }
    }
    else if (document.getElementById("CstName") != null)
    {
        if (document.getElementById("CstName").value == "")
        {
            alert("请录入“公司名称”！");
            document.getElementById("CstName").focus();
            return false;
        }
    }

    //联系方式必填
    //处理联系方式前后空格
    appForm.MobileTel.setValue(Trim(appForm.MobileTel.value));
    appForm.OfficeTel.setValue(Trim(appForm.OfficeTel.value));
    appForm.HomeTel.setValue(Trim(appForm.HomeTel.value));
    appForm.Fax.setValue(Trim(appForm.Fax.value));
    
    if (appForm.MobileTel.value == "" && appForm.OfficeTel.value == "" && appForm.HomeTel.value == "" && appForm.Fax.value == "") {
        alert("客户的“手机号码”、“家庭电话”、“公司电话”、“传真”必须填写一个！");
        return false;
    }

    var bIsValid = appForm.IsValid();
    if (bIsValid == false)
    {
        return false;
    }

    if (!ChkMobileTel(appForm.MobileTel.value))
    {
        alert("您输入的手机号码不正确，请重新输入！");
        appForm.MobileTel.focus();
        return false;
    }
  
    if (!checkEmail(appForm.Email.value))
    {
        alert("您输入的电子邮箱不正确，请重新输入！");
        appForm.Email.focus();
        return false;
    }

    if (blnLxfs)
    {
        if (GetControlValue("chkLxfs") == "")
        {
            alert("请选择联系方式！");
            return false;
        }
    }

    if (document.getElementById("CardID") != null)
    {
        if (!CardIDChanged(true))
        {
            return false;
        }
    }

    if (blnGzfmReq) {
        if (appForm.Gzfm1.value == "" && appForm.Gzfm2.value == "" && appForm.Gzfm3.value == "") {
            alert("请录入“最关注方面”！");
            appForm.Gzfm1.focus();
            return false;
        }
    }
    
    if (appForm.MainMediaName.value != "" && appForm.SubMediaName.value == "") {
        alert("请选择媒体子类！");
        appForm.SubMediaName.focus();
        return false;
    }
    
    if (!validateXqData())
    {
        return false;
    }

    
    if (blnGjxwReq)
    {
        if (GetControlValue("chkGjxw") == "")
        {
            alert('请选择“关键行为”！');
            return false;
        }
    }

    return true;
}

//收集XML并保存-新增机会页面
function getXmlAndSave_New()
{
    var sFile;
    var sDataXML = "";
    sDataXML += getP_CustomerXml();
    sDataXML += getP_CstAttributeXml();
    if (document.getElementById("__oid") == null)
    {
        sDataXML += getP_CstAttachXml();
    }
    else
    {
        sFile = "/slxt/XSZDH/Khgl_Pub_XMLHTTP.aspx?ProjGUID=" + __ProjGUID.value + "&CstGUID=" + __oid.value + "&UserGUID=" + appForm.UserGUID.value;
        strRtn = openXMLHTTP(sFile, "IsUserBelongToZygw"); //当前用户是否属于置业顾问
        if (strRtn == "0")
        {
            sDataXML += getP_CstAttachXml();
        }
    }

    sDataXML += getP_CstLog();
    sDataXML += getS_Opportunity();
    sDataXML += getS_Opp2Cst();
    sDataXML += getXqDataXML();
    sDataXML += getS_Lead();
    sDataXML += getS_gjjlXML();
    sDataXML = "<xml>" + sDataXML + "</xml>";
    sFile = "/slxt/XSZDH/Khgl_Pub_XMLHTTP.aspx?ProjGUID=" + __ProjGUID.value;
    if (document.getElementById("__OppSource") != null)
    {
        sFile += "&OppSource=" + __OppSource.value;
    }

    if (document.getElementById("__OppSource1") != null)
    {
        sFile += "&OppSource1=" + __OppSource1.value;
    }
    return openXMLHTTP(sFile, "SaveCustomer", sDataXML);
}

//收集XML并保存-编辑机会页面
function getXmlAndSave_Edit()
{
    var sFile;
    var sDataXML = "";
    sDataXML += getP_CustomerXml();
    sDataXML += getP_CstAttributeXml();
    var sFile, strRtn;
    if (appForm.appForm_OldUserGUID.value.toUpperCase() != appForm.UserGUID.value.toUpperCase())
    {
        sFile = "/slxt/XSZDH/Khgl_Pub_XMLHTTP.aspx?ProjGUID=" + __ProjGUID.value + "&CstGUID=" + __oid.value + "&UserGUID=" + appForm.UserGUID.value;
        strRtn = openXMLHTTP(sFile, "IsUserBelongToZygw"); //当前用户是否属于新置业顾问
        if (strRtn == "0")
        {
            sDataXML += getP_CstAttachXml();
            sDataXML += "<p_CstAttachDel CstGUID='" + __oid.value + "' ProjGUID='" + __ProjGUID.value + "' UserGUID ='" + appForm.appForm_OldUserGUID.value + "' />";
        }
    }

    sDataXML += getP_CstLog();
    sDataXML += getS_Opportunity(true);
    sDataXML += getXqDataXML();
    sDataXML += getS_gjjlXML();
    sDataXML = "<xml>" + sDataXML + "</xml>";
    sFile = "/slxt/XSZDH/Khgl_Pub_XMLHTTP.aspx?ProjGUID=" + __ProjGUID.value;
    if (document.getElementById("__OppSource") != null)
    {
        sFile += "&OppSource=" + __OppSource.value;
    }
	return openXMLHTTP(sFile, "SaveCustomer", sDataXML);

}

//保存后处理--新增机会页面
function afterSave_New(sRtn) {
    
    var strOppGUID = sRtn.split('|')[1];    
    var sParamList = strOppGUID + "|||Add|Add|s_Opportunity";
    var strFile = "/Slxt/PUB/Pub_XMLHTTP.aspx?IsUseByCreater=1";
    var strRtn = openXMLHTTP(strFile, "ChangeBusinessUser", sParamList);
    
    var strCstGUID = sRtn.split('|')[2];
    
    if (document.getElementById("FromLead")==null) {        
        sParamList = strCstGUID + "|||Add|Add|p_Customer";
        strFile = "/Slxt/PUB/Pub_XMLHTTP.aspx?IsUseByCreater=1";
        strRtn = openXMLHTTP(strFile, "ChangeBusinessUser", sParamList);
    } else {
        if (appForm.appForm_OldUserGUID.value != appForm.UserGUID.value) {
            var sParamList = strCstGUID + "|" + appForm.UserGUID.value + "||Modify||p_Customer";
            //这里没有删除该客户的旧置业顾问，而是新增了一个置业顾问
            //var sParamList = strCstGUID + "|" + appForm.UserGUID.value + "|" + appForm.appForm_OldUserGUID.value + "|Modify||p_Customer";
            var strFile = "/Slxt/PUB/Pub_XMLHTTP.aspx";
            var strRtn = openXMLHTTP(strFile, "ChangeBusinessUser", sParamList);
        }
    }
    


    appForm.setFormDefaultValue();
    if (blnNotFresh) return; //不执行刷新
    
    //线索转机会
    if (document.getElementById("__TransOpp") != null)
    {
        try {
            opener.parent.queryGridList();
        }
        catch (e) { }
        window.close();
    }
    else
    {
        ReturnMain();
        parent.appIframe.window.appGrid.frameElement.Query(strOppGUID);
        parent.freshTotalInfo();
    }
}


//页面是否有变化
function FormChanged() {
    blnFormChanged = true;
}

//保存后处理--编辑机会页面
function afterSave_Edit(IsRh) {

    if (appForm.appForm_OldUserGUID.value != appForm.UserGUID.value)
    {
        var sParamList = __OppGUID.value + "|" + appForm.UserGUID.value + "|" + appForm.appForm_OldUserGUID.value + "|Modify||s_Opportunity";
        var strFile = "/Slxt/PUB/Pub_XMLHTTP.aspx";
        var strRtn = openXMLHTTP(strFile, "ChangeBusinessUser", sParamList);

        var sParamList = __CstGUID.value + "|" + appForm.UserGUID.value + "|" + appForm.appForm_OldUserGUID.value + "|Modify||p_Customer";
        var strFile = "/Slxt/PUB/Pub_XMLHTTP.aspx";
        var strRtn = openXMLHTTP(strFile, "ChangeBusinessUser", sParamList);
    }

    appForm.setFormDefaultValue();
    if (blnNotFresh) return; //不执行刷新
    //IsRh表示从超链接上进行的保存
    if (!IsRh)
    {
        //flag表示从接电接访页面进入
        if (document.getElementById("__flag") != null)
        {
            ReturnMain();
            parent.appIframe.window.appGrid.frameElement.Query(__OppGUID.value);
            parent.freshTotalInfo();
        }
        else {

            try {
                opener.window._appGrid_Query();
            }
            catch (e) { }

            try {
                opener.myQuery();
            }
            catch (e) { }

            

            window.close();
        }
    }
    else {
        if (document.getElementById("__flag") != null) {            
            try
            { 
                parent.freshIframe();
                parent.freshTotalInfo();
 
            } catch (e) { }
        }
        else {
            try {
                opener.window._appGrid_Query();
                window.location.reload();
            }
            catch (e) { }
        }       
    }    
    return true;
}

function freshIframe1() {
    
    var strCstGUID = __CstGUID.value;
    var strProjGUID = __ProjGUID.value;
    var strOppGUID = __OppGUID.value;
    var strCstType = __CstType.value;

    var rdNum = Math.random();
    appForm.setFormDefaultValue(); 
    parent.document.frames('appIframe2').location = "/Slxt/XSZDH/Jdjf_Edit.aspx?mode=2&CstGUID=" + strCstGUID + "&ProjGUID=" + strProjGUID + "&OppGUID=" + strOppGUID + "&flag=1&CstType=" + escape(strCstType) + "&rdnum=" + rdNum;
}

function setAllControlReadOnly() {
    if (document.getElementById("__CstType") != null) {
        if (document.getElementById("__CstType").value == "公司") {
        }
        else {
            appForm.Gender.setReadOnly(true);
        }
    }
    appForm.MobileTel.setReadOnly(true);
    appForm.HomeTel.setReadOnly(true);
    appForm.OfficeTel.setReadOnly(true);
    appForm.Email.setReadOnly(true);
    appForm.Fax.setReadOnly(true);
    appForm.IsMainJcz.setReadOnly(true);
    appForm.FromArea.setReadOnly(true);
    appForm.HomeArea.setReadOnly(true);
    appForm.WorkArea.setReadOnly(true);
    appForm.Jzyt.setReadOnly(true);
    appForm.Family.setReadOnly(true);
    appForm.ZnNum.setReadOnly(true);
    appForm.ZyNum.setReadOnly(true);
    appForm.Work.setReadOnly(true);
    appForm.EduLevel.setReadOnly(true);
    appForm.CardID.setReadOnly(true);
    appForm.BirthDate.setReadOnly(true);
    appForm.Address.setReadOnly(true);
    appForm.PostCode.setReadOnly(true);
    appForm.HaveBM.setReadOnly(true);
    appForm.MainMediaName.setReadOnly(true);
    appForm.SubMediaName.setReadOnly(true);
    appForm.Gfys.setReadOnly(true);
    appForm.Gfyt.setReadOnly(true);

    appForm.Xqts.setReadOnly(true);
    appForm.Gzqs.setReadOnly(true);
    appForm.Gzjg.setReadOnly(true);
    appForm.Competitor.setReadOnly(true);
    appForm.Gzfm1.setReadOnly(true);
    appForm.Gzfm2.setReadOnly(true);
    appForm.Gzfm3.setReadOnly(true);
    appForm.DsType.setReadOnly(true);
    appForm.DsReason.setReadOnly(true);

    appForm.DsRemark.setReadOnly(true);
    appForm.Gjfs.setReadOnly(true);
    appForm.GjContent.setReadOnly(true);
    appForm.NextDate.setReadOnly(true);
    appForm.NextContent.setReadOnly(true);
    appForm.Remark.setReadOnly(true);
}